﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* Bank Dialog
	This dialog will ask player to consider buying the property and show player cash status
*/



package monopoly.GameController.Dialogs {
	
	import monopoly.GameController.Dialogs.IDialog;
	import monopoly.GameController.Dialogs.Templates.*;
	import monopoly.Player.Player;
	import monopoly.Field.Field;
	import monopoly.IFS.GLOBALS;
	import monopoly.Localization.*;
	import fl.controls.*;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	
	public class BankDialog extends Sprite implements IDialog
		{
		public static const ACTION_BUY	= 0;
		public static const ACTION_PASS	= 1;
		
		protected var _myPlayer:Player;
		protected var _field:Field;
		protected var _chosen_action:uint;
		protected var _dialog:BasicDialog;
		protected var _myStrings:ILocalLanguage = LocalLanguage.getInstance();
		
		public function BankDialog(player:Player, currField:Field)
			{
			_myPlayer = player;
			_field = currField;
			
			if (_myPlayer.Cash >= _field.Cost)
				{
				_dialog = new BasicDialog(_myStrings.DlgBankLabel,
														  _myStrings.DlgBankText(_field.Cost, _myPlayer.Cash),
														  _myStrings.DlgBankBuy, _myStrings.DlgBankPass);
				}
			else
				{
				_dialog = new BasicDialog(_myStrings.DlgBankShort,
														  _myStrings.DlgBankText(_field.Cost, _myPlayer.Cash),
														  _myStrings.DlgBankPass, null);
				}
			
			/* listen for dialog client respond */
			
			_dialog.addEventListener(MouseEvent.CLICK, onCmdDialogButtonClick);
			
			addChild(_dialog);
			}
		
		public override function set width(value:Number):void
			{
			_dialog.width = value;
			}
		
		protected function onCmdDialogButtonClick(e:MouseEvent):void
			{
			if (e.target is Button)
				{
				if ( (e.target as Button).label == _myStrings.DlgBankBuy )
					{
					_chosen_action = ACTION_BUY;
					}
				else if ( (e.target as Button).label == _myStrings.DlgBankPass )
					{
					_chosen_action = ACTION_PASS;
					}
			
				/* this command is done hence shutdown the dialog */
			
				e.currentTarget.removeEventListener(MouseEvent.CLICK, onCmdDialogButtonClick);
				e.currentTarget.remove( );
				removeChild(Sprite(e.currentTarget));
				
				/* signal game controller we are done */
				
				dispatchEvent(new Event(Event.COMPLETE));
				}
			}
			
		public function get chosenAction():uint
					{
					return this._chosen_action;
					}
		public function get field():Field
			{
			return _field;
			}
		public function get player():Player
			{
			return _myPlayer;
			}
		}
}